package com.example.admin.service;

import com.example.admin.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class SecurityUserService implements UserDetailsService
{
    @Autowired
    private AdminService adminService;

    //登录的时候，会把username传递到这里
    //通过username查询admin,如果存在,将密码告诉springSecurity
    //如果不存在 返回Null,代表认证失败
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
    {
        Admin admin = adminService.findAdminByUsernameAndPassword(username,null);
        if (admin == null)
        {
            //登录失败
            return null;
        }
        UserDetails userDetails = new User(username, admin.getPassword(), new ArrayList<>());
        return userDetails;
    }
}
